package com.huawei.M01.iterator.M25;

/**
 * 1574. 删除最短的子数组使剩余数组有序
 *
 * @author 111
 */
public class FIndLength {
    public int findLengthOfShortestSubarray(int[] arr) {
        int length = arr.length;
        int right = length;
        while (right > 0 && arr[right - 1] >= arr[right - 2]) {
            right--;
        }
        if (right == 0) {
            return 0;
        }
        int res = right;
        for (int i = 0; i < length; i++) {
            while (right < length - 1 && arr[right] < arr[i]) {
                right++;
            }
            res = Math.min(res, right - i - 1);
            if (i < length - 1 && arr[i] > arr[i + 1]) {
                break;
            }
        }
        return res;
    }

}